package com.smasher.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.smasher.widget.R;


/**
 * @author matao
 * @date 2019/5/7
 */
public class BlurMaskFilterView extends LinearLayout {

    private static final String TAG = "BlurMaskFilterView";
    private MaskFilter mMaskFilter;
    private Paint mPaint;
    private RectF rectF;
    private int radius;
    private int mColor;
    private int blurStart;
    private int blurEnd;
    private int blurTop;
    private int blurBottom;

    public BlurMaskFilterView(Context context) {
        this(context, null);
    }

    public BlurMaskFilterView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BlurMaskFilterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        int defaultColor = Color.parseColor("#ED424B");
        int defaultInvented = 3;
        int defaultInventedHorizontal = 5;

        try (TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BlurMaskFilterView)) {
            blurStart = array.getDimensionPixelSize(R.styleable.BlurMaskFilterView_blur_start, defaultInventedHorizontal);
            blurEnd = array.getDimensionPixelSize(R.styleable.BlurMaskFilterView_blur_end, defaultInventedHorizontal);
            blurTop = array.getDimensionPixelSize(R.styleable.BlurMaskFilterView_blur_top, defaultInvented);
            blurBottom = array.getDimensionPixelSize(R.styleable.BlurMaskFilterView_blur_bottom, defaultInvented);
            radius = array.getDimensionPixelSize(R.styleable.BlurMaskFilterView_shape_radius, defaultInvented);
            mColor = array.getColor(R.styleable.BlurMaskFilterView_blur_color, defaultColor);
        } catch (Exception e) {
            Log.e(TAG, "BlurMaskFilterView: ", e);
        }
        init();
    }


    private void init() {

        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        setWillNotDraw(false);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        rectF = new RectF();

        mMaskFilter = new BlurMaskFilter(blurStart, BlurMaskFilter.Blur.SOLID);
        setPadding(blurStart, blurTop, blurEnd, blurBottom);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        Log.d(TAG, "onDraw: " + blurStart);
        super.onDraw(canvas);

        rectF.set(blurStart, blurTop, getWidth() - blurEnd, getHeight() - blurBottom);

        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(mColor);
        mPaint.setMaskFilter(mMaskFilter);
        mPaint.setStrokeWidth(5);
        canvas.drawRoundRect(rectF, radius, radius, mPaint);
    }
}
